package me.numark.view;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import me.numark.R;

/**
 * Created by tian on 14-9-21:下午10:28.
 */
public class WarningWindow {

    Context mContext;

    PopupWindow mWindow;

    View mRootView;

    private String mTitle;

    private String mMessage;

    public WarningWindow(Context context, String title, String message) {
        mContext = context;
        mTitle = title;
        mMessage = message;
        mWindow = new PopupWindow(context);

        mWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mWindow.dismiss();
                return true;
            }
        });

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.warning_window, null);
        TextView titleView = (TextView) mRootView.findViewById(R.id.title);
        titleView.setText(mTitle);
        TextView messageView = (TextView) mRootView.findViewById(R.id.message);
        messageView.setText(mMessage);
        mRootView
                .setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
        mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        mWindow.setTouchable(true);
        mWindow.setFocusable(true);
        mWindow.setOutsideTouchable(true);
        mWindow.setContentView(mRootView);
    }

    public void show(View anchor) {
        mWindow.showAtLocation(anchor, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, -50);
    }


}
